package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.pojo.Coupon;
import com.example.demo.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/Coupon")
@Api(value = "优惠券管理",tags = "优惠券管理")
public class CouponController {
    @Resource
    private CouponService couponService;
    @RequestMapping(value = "/deleteByPrimaryKey",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除优惠券",notes = "删除优惠券")
    @ApiImplicitParams({@ApiImplicitParam(name = "couponid",value = "优惠券id")})
    public String deleteByPrimaryKey(Integer couponid){
        int res=couponService.deleteByPrimaryKey(couponid);
        return JSON.toJSONString(res);
    }

    @ResponseBody
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation(value = "添加优惠券",notes = "添加优惠券")
    @ApiImplicitParams({@ApiImplicitParam(name = "couponname",value = "优惠券名称"),
                        @ApiImplicitParam(name = "discountmonay",value = "优惠券金额"),
                        @ApiImplicitParam(name = "usescope",value = "使用范围"),
                        @ApiImplicitParam(name = "effectivetime",value = "有效时间"),
                        @ApiImplicitParam(name = "type",value = "类型")})
    public String insert(Coupon record){
        int res=couponService.insert(record);
        return JSON.toJSONString(res);
    }
    @ResponseBody
    @RequestMapping(value = "/getAll",method = RequestMethod.POST)
    @ApiOperation(value = "查看所有优惠券",notes = "查看所有优惠券")
    public String getAll(){
        List<Coupon> list=couponService.getAll();
        return JSON.toJSONString(list);
    }
    @ResponseBody
    @RequestMapping(value = "/updateByPrimaryKeySelective",method = RequestMethod.POST)
    @ApiOperation(value = "修改优惠券",notes = "修改优惠券")
    @ApiImplicitParams({@ApiImplicitParam(name = "couponname",value = "优惠券名称"),
            @ApiImplicitParam(name = "discountmonay",value = "优惠券金额"),
            @ApiImplicitParam(name = "usescope",value = "使用范围"),
            @ApiImplicitParam(name = "effectivetime",value = "有效时间"),
            @ApiImplicitParam(name = "type",value = "类型")})
    public String updateByPrimaryKeySelective(Coupon record){
        int res=couponService.updateByPrimaryKeySelective(record);
        return JSON.toJSONString(res);
    }
    @RequestMapping(value = "/getAllHou")
    public String getAllHou(Model model){
        List<Coupon> list=couponService.getAll();
        model.addAttribute("getAll",list);
        return "news";
    }
    @RequestMapping("/news")
    public String newDo(){
        return "news";
    }
    @RequestMapping(value = "/deleteByPrimaryKeyEr")
    @ApiOperation(value = "删除优惠券",notes = "删除优惠券")
    @ApiImplicitParams({@ApiImplicitParam(name = "couponid",value = "优惠券id")})
    public String deleteByPrimaryKeyEr(Integer couponid){
        int res=couponService.deleteByPrimaryKey(couponid);
        return "redirect:/Coupon/getAllHou";
    }
    @RequestMapping(value = "/couponInsert")
    public String couponInsert(Coupon coupon){
        System.out.println(coupon.getCouponname());
        int res=couponService.insert(coupon);
        return "redirect:/Coupon/getAllHou";
    }
    @RequestMapping("/couponInsertDo")
    public String couponInsertDo(){
        return "couponInsert";
    }
}
